/*
 * FortressData - contains information about fortress pieces - health,
 * weapons, etc.
 *
 * Copyright 2008 James W. Hofmann
 *
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 */

package rules;

class FortressData
{
	
	inline public static var fort_line = 0;
	inline public static var fort_base = 1;
	inline public static var fort_tower = 2;
	
	inline public static var fort_maxhealth = 100 * 30;

	inline public static var cost_line = -200;
	inline public static var cost_tower = -2000;
		
	public var style : Int;
	public var health : Int;

	public var id : Int;

	public var x : Int;
	public var y : Int;

	public var shooter : Shooter;
	
	public function new(style : Int)
	{
		this.style = style;
		this.health = fort_maxhealth;

		var fortdatas = RulesAPI.g.fortdatas;
		this.id = fortdatas.setnext(this);

		GameResponse.addcomposition(
		new GameResponse(GameResponse.msg_fortadd, [this, 
		this.id])
		);
		
	}
		
	public function hurt(qty : Int)
	{
			GameResponse.addcomposition(
		new GameResponse(GameResponse.msg_fortdamage, [this, qty])
		);	
		this.health-=qty;
		if (health<=0)
			kill();
	}

	public function kill()
	{
			GameResponse.addcomposition(
		new GameResponse(GameResponse.msg_fortkill, this)
		);	
		RulesAPI.g.fortdatas.remove(this.id);
		RulesAPI.g.m.tiles[x][y].fort = null;
		if (this.style == fort_base)
		{
			RulesAPI.g.gameover=true;
			GameResponse.addcomposition(
			new GameResponse(GameResponse.msg_gameover, null)
			);				
		}
	}
	
	public function up()
	{
		health += 1;
		if (health>fort_maxhealth)
			health = fort_maxhealth;
		else
			GameResponse.addcomposition(
		new GameResponse(GameResponse.msg_fortrepair, this)
		);
		if (shooter!=null)
			shooter.up();
		if (RulesAPI.g.m.tiles[x][y].terrain==GameTile.ter_energy)
			FortressData.changeenergy(1);
	}
	
	public static function changeenergy(qty : Int)
	{
		RulesAPI.g.energy += qty;
		GameResponse.addcomposition(
		new GameResponse(GameResponse.msg_energychange, qty)
		);
	}
	
}
